package cn.sccl.dirms.security.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.security.model.Unit;
import cn.sccl.dirms.security.service.UnitManager;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 机构ID 的Controller
* Created by P650RE3 on 2016-10-19.
*/
@RestController
@RequestMapping(value = "/dirms/security/unit")
public class UnitController extends BaseController {

@Autowired
private UnitManager unitManager;

/**
* 显示所有数据
*/
@RequestMapping(value = "/all" , method = RequestMethod.GET)
public List<Unit> listAllUnit(){
return unitManager.listAll();
}

/**
* 分页查询，分页参数放置在url路径中
* @param pageIndex 页码
* @param pageSize 分页大小
* @param request
* @return
*/
@RequestMapping(value = "/page/{pageIndex}/{pageSize}" , method = RequestMethod.GET)
public PageResult<Unit> pageQueryUnit(@PathVariable("pageIndex") long pageIndex , @PathVariable("pageSize") int pageSize , HttpServletRequest request){
return unitManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
}

/**
* 分页查询，分页参数放置在request中
* @param request
* @return
*/
@RequestMapping(value = "/page" , method = RequestMethod.GET )
public PageResult<Unit> pageQueryUnit(HttpServletRequest request){
return unitManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
}

/**
* 保存数据
* @param unit
* @return
*/
@RequestMapping(value = "/save" , method = RequestMethod.POST)
public Unit saveUnit(@RequestBody Unit unit){
unitManager.save(unit);
return unit;
}

/**
* 根据ID删除数据
* @param id
*/
@RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
@JsonView(Object.class)
public void deleteUnit(@PathVariable("id") String id){
unitManager.delete(id);
}

    /**
     * 查询企业列表
     * @param
     * @return
     */
    @RequestMapping(value = "/findUnit" , method = RequestMethod.GET )
    public List<Unit> findUnit(){
        return unitManager.findUnit();
    }


    /**
     * 根据设备序列号，查询用户首页设置
     * @return
     */
    @RequestMapping(value = "/findUnitByunNo/{unNo}" , method = RequestMethod.GET)
    public Unit findUnitByunNo(@PathVariable("unNo") String unNo){
        return unitManager.findUnitByStuUnNo(unNo);
    }
}
